package com.example.mumu.passwordbook;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.mumu.passwordbook.dao.AccountDao;
import com.example.mumu.passwordbook.domain.Account;
import com.example.mumu.passwordbook.domain.IconEnum;
import com.example.mumu.passwordbook.domain.TypeEnum;

/**
 * Created by lupan 2018/6/14
 */
public class AddAccountActivity extends AppCompatActivity {

    private AccountDao accountDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_edit_account);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        //关键下面两句话，设置了回退按钮，及点击事件的效果
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        accountDao = new AccountDao(this);

        final TextInputLayout til_name = findViewById(R.id.til_name);
        final TextInputLayout til_username = findViewById(R.id.til_username);
        final TextInputLayout til_password = findViewById(R.id.til_password);

        final TextInputEditText tie_name = findViewById(R.id.tie_name);
        final TextInputEditText tie_username = findViewById(R.id.tie_username);
        final TextInputEditText tie_password = findViewById(R.id.tie_password);
        final TextInputEditText tie_remark = findViewById(R.id.tie_remark);
        final AppCompatSpinner  sp_type = findViewById(R.id.sp_type);
        final ImageView iv_icon = findViewById(R.id.iv_icon);

        //设置菜单项点击响应
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                boolean isValid = true;
                if (TextUtils.isEmpty(tie_name.getText())) {
                    til_name.setError(getResources().getString(R.string.name_error));
                    isValid = false;
                }
                if (TextUtils.isEmpty(tie_username.getText())) {
                    til_username.setError(getResources().getString(R.string.username_error));
                    isValid = false;
                }
                if (TextUtils.isEmpty(tie_password.getText())) {
                    til_password.setError(getResources().getString(R.string.password_error));
                    isValid = false;
                }

                if (isValid) {
                    //清空错误提示
                    til_name.setError(null);
                    til_password.setError(null);
                    til_username.setError(null);

                    //保存账户
                    Account account = new Account();
                    account.setName(tie_name.getText().toString());
                    account.setUsername(tie_username.getText().toString());
                    account.setPassword(tie_password.getText().toString());
                    if(!TextUtils.isEmpty(tie_remark.getText())) {
                        account.setRemark(tie_remark.getText().toString());
                    }
                    Object type = sp_type.getSelectedItem();
                    account.setType(TypeEnum.textOf((String)type));
                    account.setIcon(IconEnum.valueOf((String)iv_icon.getTag()));

                    accountDao.insert(account);

                    Toast.makeText(AddAccountActivity.this,R.string.add_account_success,Toast.LENGTH_SHORT).show();
                    AddAccountActivity.this.setResult(1);
                    AddAccountActivity.this.finish();
                }
                return false;
            }
        });

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddAccountActivity.this, SelectAccountIconActivity.class);
                startActivityForResult(intent, 1);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.add_edit_account, menu);
        return true;
    }
}
